﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visifire.Charts;
using System.Windows.Media.Imaging;
using System.IO;

namespace GisWeb
{
    public partial class comWindow : FloatableWindow
    {
       public int showCom = 0;
        public comWindow()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int type = (sender as ComboBox).SelectedIndex;
            if(showCom==1){
                if (gdp_com.Children.Count > 0)
                {

                    Chart gdpChart = gdp_com.Children[0] as Chart;
                    //Chart perChart = PERchart.Children[0] as Chart;
                    Chart popu = popu_com.Children[0] as Chart;
                    Chart per = per_com.Children[0] as Chart;
                    switch (type)
                    {
                        case 1:

                            gdpChart.View3D = true;
                            foreach (DataSeries i in gdpChart.Series)
                            {
                               i.RenderAs = RenderAs.Column;
                            }
                            per.View3D = true;
                            foreach (DataSeries i in per.Series)
                            {
                                i.RenderAs = RenderAs.Column;
                            }
                            // perChart.Series[0].RenderAs = RenderAs.Column;
                            popu.View3D = true;
                            foreach (DataSeries i in popu.Series)
                            {
                                i.RenderAs = RenderAs.Column;
                            }
                            
                            break;
                        case 2:
                            gdpChart.View3D = true;
                     
                            foreach (DataSeries i in gdpChart.Series)
                            {
                                i.RenderAs = RenderAs.Bar;
                            }
                             per.View3D = true;
                            foreach (DataSeries i in per.Series)
                            {
                                i.RenderAs = RenderAs.Bar;
                            }
                            //  perChart.Series[0].RenderAs = RenderAs.Pie;
                            popu.View3D = true;
                            foreach (DataSeries i in popu.Series)
                            {
                                i.RenderAs = RenderAs.Bar;
                            }
                            break;
                        case 0:
                            foreach (DataSeries i in gdpChart.Series)
                            {
                                i.RenderAs = RenderAs.Line;
                            }
                            foreach (DataSeries i in per.Series)
                            {
                                i.RenderAs = RenderAs.Line;
                            }
                            foreach (DataSeries i in popu.Series)
                            {
                                i.RenderAs = RenderAs.Line;
                            }
                            //     perChart.Series[0].RenderAs = RenderAs.Line;
                 
                            break;
                        case 3:
                            gdpChart.View3D = true;
                            popu.View3D = true;
                             per.View3D = true;
                            foreach (DataSeries i in per.Series)
                            {
                                i.RenderAs = RenderAs.StackedColumn;
                            }
                            foreach (DataSeries i in gdpChart.Series)
                            {
                                i.RenderAs = RenderAs.StackedColumn;
                            }
         
                            //     perChart.Series[0].RenderAs = RenderAs.Line;
                     
                            foreach (DataSeries i in popu.Series)
                            {
                                i.RenderAs = RenderAs.StackedColumn;
                            }
                            break;
                        case 4:
                            gdpChart.View3D = true;
                            popu.View3D = true;
                            per.View3D = true;
                            foreach (DataSeries i in per.Series)
                            {
                                i.RenderAs = RenderAs.StackedArea;
                            }
                            foreach (DataSeries i in gdpChart.Series)
                            {
                                i.RenderAs = RenderAs.StackedArea;
                            }

                            //     perChart.Series[0].RenderAs = RenderAs.Line;

                            foreach (DataSeries i in popu.Series)
                            {
                                i.RenderAs = RenderAs.StackedArea;
                            }
                            break;
                    }
                }
            }
        }


         private void Button_Click(object sender, RoutedEventArgs e)
        {
          
            int index = tab.SelectedIndex;
       
                switch (index) {
                    case 0: 
                       //SaveToImage(GDPchart.Children[0] as Chart);
                        saveImg(gdp_com);
                       break;
                    case 1:
                       saveImg(per_com);break;
                    case 2:
                        saveImg(popu_com);break;
                    case 3: 
                          //SaveToImage(POPUchart.Children[0] as Chart); 
                        saveImg(ind_com);
                          break;
                
                
                
                }
            //var gt = GDPchart.TransformToVisual(null);

            //Point p = gt.Transform(new Point(0, 0));
            //p.X = p.X + GDPchart.ActualWidth;
            //p.Y = p.Y + GDPchart.ActualHeight;

            //MessageBox.Show(p.X + "坐标" + p.Y);
          
        }

        //private void SaveToImage(Chart chart)
        //{
        //    try
        //    {
        //        WriteableBitmap bitmap = new WriteableBitmap(chart, null);

        //        if (bitmap != null)
        //        {
        //            SaveFileDialog saveDlg = new SaveFileDialog();
        //            saveDlg.Filter = "JPEG Files (*.jpeg)|*.jpeg";
        //            saveDlg.DefaultExt = ".jpeg";

        //            if ((bool)saveDlg.ShowDialog())
        //            {
        //                using (Stream fs = saveDlg.OpenFile())
        //                {
        //                    MemoryStream stream = GetImageStream(bitmap);

        //                    //Get Bytes from memory stream and write into IO stream
        //                    byte[] binaryData = new Byte[stream.Length];
        //                    long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
        //                    fs.Write(binaryData, 0, binaryData.Length);
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Diagnostics.Debug.WriteLine("Note: Please make sure that Height and Width of the chart is set properly.");
        //        System.Diagnostics.Debug.WriteLine(ex.Message);
        //    }
        //}
        //public static MemoryStream GetImageStream(WriteableBitmap bitmap)
        //{
        //    byte[][,] raster = ReadRasterInformation(bitmap);
        //    return EncodeRasterInformationToStream(raster, ColorSpace.RGB);
        //}
        //public static byte[][,] ReadRasterInformation(WriteableBitmap bitmap)
        //{
        //    int width = bitmap.PixelWidth;
        //    int height = bitmap.PixelHeight;
        //    int bands = 3;
        //    byte[][,] raster = new byte[bands][,];

        //    for (int i = 0; i < bands; i++)
        //    {
        //        raster[i] = new byte[width, height];
        //    }
        //    for (int row = 0; row < height; row++)
        //    {
        //        for (int column = 0; column < width; column++)
        //        {
        //            int pixel = bitmap.Pixels[width * row + column];
        //            raster[0][column, row] = (byte)(pixel >> 16);
        //            raster[1][column, row] = (byte)(pixel >> 8);
        //            raster[2][column, row] = (byte)pixel;
        //        }
        //    }

        //    return raster;
        //}
        //public static MemoryStream EncodeRasterInformationToStream(byte[][,] raster, ColorSpace colorSpace)
        //{
        //    ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
        //    FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

        //    //Encode the Image as a JPEG
        //    MemoryStream stream = new MemoryStream();
        //    FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
        //    encoder.Encode();

        //    // Back to the start
        //    stream.Seek(0, SeekOrigin.Begin);
        //    return stream;
        //}

        private void saveImg(UIElement grid) {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "支持的图像文文件(*.png)|*.png";
            if (dialog.ShowDialog() != true)
            {
                return;
            }
            System.Windows.Media.Imaging.WriteableBitmap bitmap = new WriteableBitmap(grid, null);
            int[] i = bitmap.Pixels;
            EditableImage imageData = new EditableImage(bitmap.PixelWidth, bitmap.PixelHeight);


            for (int y = 0; y < bitmap.PixelHeight; ++y)
            {
                for (int x = 0; x < bitmap.PixelWidth; ++x)
                {

                    int pixel = i[bitmap.PixelWidth * y + x];

                    imageData.SetPixel(x, y,
                                (byte)((pixel >> 16) & 0xFF),
                                (byte)((pixel >> 8) & 0xFF),
                                (byte)(pixel & 0xFF),
                                (byte)((pixel >> 24) & 0xFF)
                                );
                }
            }
            Stream pngStream = imageData.GetStream();

            byte[] buffer = new byte[pngStream.Length];
            pngStream.Read(buffer, 0, (int)pngStream.Length);
            pngStream.Dispose();

            Stream st = dialog.OpenFile();
            st.Write(buffer, 0, buffer.Length);
            st.Close();
        }
    
    }



   

   
}

    




